﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using System.Text;

namespace Actuals.Data
{
    public class DalPublicHoliday : Dal<IPublicHoliday, PublicHoliday>, IDalPublicHoliday
    {

          private readonly IDalManager _manager;
        private static readonly IDalPublicHoliday _instance = new DalPublicHoliday();
        private const string SP_GET_PUBLICHOLIDAYS = "usp_GetPublicHolidays";
        private const string SP_ADD_AND_UPDATE_PUBLICHOLIDAYS = "usp_Add_And_Update_PublicHolidays";
        private const string SP_GET_PUBHOLIDAYS = "usp_GetPubHolidays";
        private const string SP_GET_PUBLICHOLIDAY = "usp_GetPublicHoliday";
        private const string SP_DELETE_PUBLICHOLIDAYS = "usp_Delete_publicHolidays";


        public static IDalPublicHoliday Instance
        {
            get { return _instance; }
        }

        public DalPublicHoliday()
        {
            _manager = new DalManager();
        }

        public DalPublicHoliday(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IPublicHoliday obj, DataRow row)
        {
            obj.Id = _manager.GetValue<int>(row, "Id");
            obj.HolidayDate = _manager.GetValue<DateTime>(row, "HolidayDate");
            obj.Description = _manager.GetValue<string>(row, "Description");

        }
        public IList<IPublicHoliday> GetPublicHolidays(DateTime fromDate, DateTime toDate)
        {
            _manager.StoredProcedureName = SP_GET_PUBLICHOLIDAYS;
            _manager.AddInParameter("@FromDate", DbType.DateTime, fromDate);
            _manager.AddInParameter("@ToDate", DbType.DateTime, toDate);

            List<IPublicHoliday> publicHolidays = new List<IPublicHoliday>();
            foreach (IPublicHoliday publicHoliday in ConvertAll(_manager.ExecuteDataSet()))
                publicHolidays.Add(publicHoliday);

            return publicHolidays;
        }

        public bool AddAndUpdatePublicHolidays(IPublicHoliday publicHoliday)
        {
            _manager.StoredProcedureName = SP_ADD_AND_UPDATE_PUBLICHOLIDAYS;
            _manager.AddInParameter("@Id", DbType.Int32, publicHoliday.Id);
            _manager.AddInParameter("@HolidayDate", DbType.DateTime, publicHoliday.HolidayDate);
            _manager.AddInParameter("@Description", DbType.String, publicHoliday.Description);
            _manager.ExecuteNonQuery();
            return true;
        }
        public bool DeletePublicHolidays(int id)
        {
            _manager.StoredProcedureName = SP_DELETE_PUBLICHOLIDAYS;
            _manager.AddInParameter("@Id", DbType.Int32, id);
            _manager.ExecuteNonQuery();
            return true;

        }
        public IList<IPublicHoliday> GetPubHolidays(int year)
        {
            _manager.StoredProcedureName = SP_GET_PUBHOLIDAYS;
            _manager.AddInParameter("@Year", DbType.Int32, year);
            List<IPublicHoliday> publicHolidays = new List<IPublicHoliday>();
            foreach (IPublicHoliday publicHoliday in ConvertAll(_manager.ExecuteDataSet()))
                publicHolidays.Add(publicHoliday);
            return publicHolidays;
        }

        public IPublicHoliday GetPublicHoliday(int id)
        {
            _manager.StoredProcedureName = SP_GET_PUBLICHOLIDAY;
            _manager.AddInParameter("@Id", DbType.Int32,id);
            IPublicHoliday publicHoliday = Convert(_manager.ExecuteDataSet());
            return publicHoliday;

        }

    }

       

}
